package com.sly.demoim.helper;

import com.sly.demoim.constant.ChatRedisConst;
import com.sly.demoim.model.connect.UserConnectInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 聊天redis帮助类
 *
 * @author SLY
 * @date 2022/7/18
 */
@Slf4j
@Component
public class ChatRedisHelper {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 获取用户连接信息
     *
     * @param userId 用户ID
     * @return {@link UserConnectInfo}
     * @author SLY
     * @date 2022/7/18
     */
    public UserConnectInfo findUserConnectInfo(String userId) {
        return (UserConnectInfo) redisTemplate.opsForValue().get(ChatRedisConst.CONNECT_INFO_PREFIX + userId);
    }

    /**
     * 保存用户连接信息
     *
     * @param userConnectInfo 用户连接信息
     * @author SLY
     * @date 2022/7/18
     */
    public void saveUserConnectInfo(UserConnectInfo userConnectInfo) {
        redisTemplate.opsForValue().set(ChatRedisConst.CONNECT_INFO_PREFIX + userConnectInfo.getUserId(), userConnectInfo);
    }

}
